package com.zzb.projectfactory.framework.role.service;

import com.alibaba.fastjson.JSON;
import com.zzb.projectfactory.framework.role.dao.SysRoleDAO;
import com.zzb.projectfactory.framework.role.dao.jpa.SysRoleRepository;
import com.zzb.projectfactory.framework.role.entity.SysRole;
import com.zzb.projectfactory.framework.role.entity.User;
import com.zzb.projectfactory.framework.common.BaseResponse;
import com.zzb.projectfactory.framework.common.BaseService;
import com.zzb.projectfactory.framework.common.ServiceException;
import com.zzb.projectfactory.framework.validator.annotation.ValidatorEnabled;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* 角色表 service类
*/
@Service
public class SysRoleService extends BaseService<SysRoleDAO,SysRole,SysRoleRepository> {
    private static final Logger logger = LoggerFactory.getLogger(SysRoleService.class);
    @Autowired
    private SysRoleDAO sysRoleDAO;
    @Override
    public SysRoleDAO getDAO() {
        return sysRoleDAO;
    }
    /**删除角色表*/
    public BaseResponse deleteRole(String id,User operate) throws ServiceException {
        //TODO 删除应该不仅仅这么简单的操作，后面想到了再加 ZZB 20170222
        return delete(id);
    }
    /**保存角色表*/
    @ValidatorEnabled
    public BaseResponse<SysRole> saveRole(SysRole sysRole, User operator) throws ServiceException{
        logger.info("正在保存角色表["+ JSON.toJSONString(sysRole)+"]["+JSON.toJSONString(operator)+"]");
        SysRole exist = getDAO().findByRoleCode(sysRole.getRoleCode());
        if(exist != null) return new BaseResponse<>(BaseResponse.FAIL,"创建角色失败，不能创建相同角色代码的角色");
        //TODO 保存应该不仅仅这么简单的操作，后面想到了再加 ZZB 20170222
        return createOrUpdate(sysRole,operator);
    }

    public List<SysRole> findByUserId(String userId){
        return getDAO().findByUserId(userId);
    }
    public SysRole findByRoleCode(String roleCode){return getDAO().findByRoleCode(roleCode);}
}
